/*
 * PlayerHolder.h
 *
 *  Created on: 2018年7月25日
 *      Author: terry
 */

#ifndef PLAYERHOLDER_H_
#define PLAYERHOLDER_H_

#include "LitePlayer.h"
#include "SharedPtr.h"

#include "LibMediaDecoder.h"
#include "TCriticalSection.h"
#include "MediaEventSink.h"

#include "ScaleContext.h"
#include "MediaStream.h"
#include "TByteBuffer.h"
#include "ParamSet.h"

typedef std::shared_ptr< av::LitePlayer >		LitePlayerPtr;
typedef std::shared_ptr< av::InputPlayer >		InputPlayerPtr;


class PlayerHolder;
typedef std::shared_ptr< PlayerHolder >			PlayerHolderPtr;



class PlayerHolder : public av::MediaEventSink, av::MediaFrameSink
{
public:
	static int FACE_TRACK_DURATION;

public:
	PlayerHolder();
	virtual ~PlayerHolder();

	void close();

	bool isOpen();

	bool openStream(const MDecode_Format* fmt);

	bool openFile(const char* url, const char* params);


	bool inputData(int mediaType, const uint8_t* buffer, int length, int64_t pts, int flags);

	void resetBuffer();

	bool setVideoWnd(HWND hwnd);

	bool getPictureSize(int& width, int& height);

	bool snap(const char* filename);

	void refresh();

	bool play();

	bool pause(bool toPause);

	void stop();

	bool setDecodeCallback(MDecode_PixelFormat pixelFormat, MediaDecodeCallback cb, void* pUser);

	bool setDrawCallback(MediaDrawCallback cb, void* pUser);

	double getPos();

	bool setPos(double pos);

	int getDuration();

	bool setScale(double scale);

	double getScale();

	bool setFileEndMsg(HWND hwnd, uint32_t msg);

	bool openSound();

	void closeSound();

	bool isSoundOpen();

	float getVolume();

	bool setVolume(float vol);

	int64_t getTime();

	bool seek(int64_t pts);

	bool setRatio(int cx, int cy);

	bool setEventCallback(DecoderEventCallback cb, void* pUser);

	bool setVideoFilter(const char* filterString);

	bool startRecord(const char* filepath);
	void stopRecord();
	bool isRecording();

	bool setDelayTime(int milliseconds);

	bool setAudioFormat(int codec, int channels, int samplerate, const uint8_t* config, int configSize);

	bool setParam(const char* name, const char* value);

	bool setDecodeCallbackFreq(int freq);

	void setFullscreen(bool isFull);
	bool isFullscreen();

	void enableDrawGraph(int mode);
	void setDrawGraph(const char* value, size_t size);
	const char* getDrawGraph();

	void removeDrawGraph(int mode);

protected:
	virtual void onMediaEvent(void* object, int type, const char* value, int size);

    virtual void onMediaFrameFormat(const MFormat& format);
    virtual void onMediaFrame(MFrame& frame);
    virtual void onMediaFrameEnd();

protected:
	void fireEvent(int event, const char* value, int size);

	void fireWndMsg(int event);

protected:
	LitePlayerPtr	m_player;
	InputPlayerPtr	m_input;

	DecoderEventCallback	m_eventCb;
	void*	m_eventUser;

	HWND m_msgWnd;
	uint32_t m_msg;

	MDecode_PixelFormat m_pixelFormat;
	MediaDecodeCallback m_decodeCb;
	void* m_decodeUser;

	comn::CriticalSection	m_cs;

	ScaleContext	m_scaleContext;
	av::AVFramePtr	m_srcFrame;
	av::AVFramePtr	m_outFrame;

	int	m_frameCount;	/// 视频帧统计数.
	int	m_decodeFreq;	

	util::ParamSet	m_paramSet;

};



#endif /* PLAYERHOLDER_H_ */
